package com.example.oldkingdom.areas;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
import com.example.oldkingdom.activities.Explore;
import com.example.oldkingdom.base.worldObject;
import com.example.oldkingdom.base.worldPoint;
import com.example.oldkingdom.entities.spawner;
import com.example.oldkingdom.items.item;

public class mapArea 
{
	public mapArea()
	{
		
	}
	
	
	public mapArea setDesc(String text)
	{
		Description = text;
		return this;
	}
	
	public mapArea setBackground(Drawable _drawableItem)
	{
		Background = _drawableItem;
		return this;
	}
	
	public mapArea setWorldPoint(int x, int y)
	{
		return setWorldPoint(new worldPoint(x,y));
	}
	public mapArea setWorldPoint(worldPoint wp)
	{
		WorldPoint = wp;
		areas.getInstance().addArea(wp, this);
		return this;
	}
	public mapArea addItem(String item)
	{
		_items.add(item);
		return this;
	}
	
	public mapArea addSpawner(spawner s)
	{
		_spawners.add(s);
		return this;
	}
	
	
    public void look()
    {
    	String west = "";
		String east = "";
		String north = "";
		String south = "";
		if (areas.getInstance().getArea(WorldPoint.plus(worldPoint.West)) != null)
			west = ", West";
		if (areas.getInstance().getArea(WorldPoint.plus(worldPoint.East)) != null)
			east = ", East";
		if (areas.getInstance().getArea(WorldPoint.plus(worldPoint.South)) != null)
			south = ", South";
		if (areas.getInstance().getArea(WorldPoint.plus(worldPoint.North)) != null)
			north = ", North";
		String itemsDescription = "";
		ArrayList<worldObject> _visible = new ArrayList<worldObject>();
		for (spawner _spawner : _spawners)
			if (_spawner.Visible)
				_visible.add(_spawner);
		
		for (String _itemName: _items)
			_visible.add(getItem(_itemName));
		
		if (_visible.size() > 1)
		{
			itemsDescription = "You also see a";
			for (int i = 0; i < _visible.size() -1; i++)
			{
				itemsDescription += ", " + _visible.get(i).getName();
			}
			itemsDescription += " and a " + _visible.get(_visible.size() -1).getName();
		}
		else if (_visible.size() > 0)
		{
			itemsDescription = "You see a " + _visible.get(0).getName();
		
		}
		Explore.Instance.addDescription(Description);
		Explore.Instance.addDescription("You see exits to the" + west + east + south + north);
		if (itemsDescription.length() > 0)
			Explore.Instance.addDescription(itemsDescription);
		Explore.Instance.addDescription("");//space
    }
	
	public void enterArea()
	{
		if (Background != null)
		{
			Explore.Instance.setBackground(Background);
		}
		
		look();
		
		for (spawner _spawner : _spawners)
			_spawner.onEnterArea();
	}
	
	public void leaveArea()
	{
		for (spawner _spawner : _spawners)
			_spawner.onLeaveArea();
	}
	
	protected item getItem(String itemName)
	{
		for (item _item : item.allItems)
			if (_item.getName() == itemName)
				return _item;
		return null;
	}
	
	public ArrayList<String> getAllItems()
	{
		return _items;
	}
	protected ArrayList<String> _items = new ArrayList<String>();
	protected ArrayList<spawner> _spawners = new ArrayList<spawner>();
	public String Description = "";
	public Drawable Background = null;
	public worldPoint WorldPoint;
}
